home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / ProgramF / CRYSTAL / CRW9 / DEV / INCLUDE / CRDATES.C next >
Encoding:
C/C++ Source or Header  |  2002-02-20  |  3.1 KB  |  118 lines

  1. /*
  2. ** File:    CRDates.c
  3. **
  4. ** Editor:  Ron Hayter
  5. ** Date:    93-06-22
  6. **
  7. ** Purpose: Functions to convert to and from Crystal Reports' date values.
  8. **
  9. ** Copyright (c) 1993-2002  Crystal Decisions, Inc.
  10. */
  11.  
  12. #include "crdates.h"
  13.  
  14. static CRInt16u DaysInMonth [13] =
  15. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  16.  
  17. static CRInt32s YMDToJulian (CRInt16s year,
  18.                              CRInt16u month,
  19.                              CRInt16u day);
  20.  
  21. CRDate CRYearMonthDayToDate (CRInt16s year,
  22.                              CRInt16u month,
  23.                              CRInt16u day)
  24. {
  25.     CRBoolean leapYear;
  26.  
  27.     if (year < -4713)
  28.         return CRNullDate;
  29.  
  30.     leapYear =
  31.         year >= 1582 ?
  32.             (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) :
  33.             (year % 4 == 0);
  34.  
  35.     if (month < 1 || month > 12)
  36.         return CRNullDate;
  37.  
  38.     DaysInMonth [2] = leapYear ? 29 : 28;
  39.     if (day < 1 || day > DaysInMonth [month])
  40.         return CRNullDate;
  41.  
  42.     return (CRDate) YMDToJulian (year, month, day);
  43. }
  44.  
  45. static CRInt32s YMDToJulian (CRInt16s year,
  46.                              CRInt16u month,
  47.                              CRInt16u day)
  48. {
  49.     CRInt16s a, b = 0;
  50.     CRInt16u work_month = month, work_day = day;
  51.     CRInt16s work_year = year;
  52.  
  53.     /*  correct for negative year */
  54.     if (work_year < 0)
  55.         work_year++;
  56.  
  57.     if (work_month <= 2)
  58.     {
  59.         work_year--;
  60.         work_month += 12;
  61.     }
  62.  
  63.     /*  deal with Gregorian calendar */
  64.     if (work_year * 10000. + work_month * 100. + work_day >= 15821015.)
  65.     {
  66.         a = work_year / 100.;
  67.         b = 2 - a + a / 4;
  68.     }
  69.  
  70.     return (CRInt32s) (365.25 * work_year) +
  71.            (CRInt32s) (30.6001 * (work_month + 1)) +
  72.            work_day + 1720994L + b;
  73. }
  74.  
  75. void CRDateToYearMonthDay (CRDate date,
  76.                            CRInt16s *year,
  77.                            CRInt16u *month,
  78.                            CRInt16u *day)
  79. {
  80.     long a, b, c, d, e, z, alpha;
  81.     z = date + 1;
  82.  
  83.     /* dealing with Gregorian calendar reform */
  84.     if (z < 2299161L)
  85.         a = z;
  86.     else
  87.     {
  88.         alpha = (long) ((z-1867216.25) / 36524.25);
  89.         a = z + 1 + alpha - alpha / 4;
  90.     }
  91.     b = (a > 1721423L ? a + 1524 : a + 1158);
  92.     c = (long) ((b - 122.1) / 365.25);
  93.     d = (long) (365.25 * c);
  94.     e = (long) ((b - d) / 30.6001);
  95.     *day = (CRInt16u) (b - d - (long) (30.6001 * e));
  96.     *month = (CRInt16u) ((e < 13.5) ? e - 1 : e - 13);
  97.     *year = (CRInt16s) ((*month > 2.5 ) ? (c - 4716) : c - 4715);
  98. }
  99.  
  100. CRTime CRHourMinuteSecondToTime (CRInt16u hour,
  101.                                  CRInt16u minute,
  102.                                  CRInt16u second)
  103. {
  104.     return (CRTime) hour * 3600 + minute * 60 + second;
  105. }
  106.  
  107. void CRTimeToHourMinuteSecond (CRTime time,
  108.                                CRInt16u *hour,
  109.                                CRInt16u *minute,
  110.                                CRInt16u *second)
  111. {
  112.     *hour = (CRInt16u) (time / 3600);
  113.     *minute = (CRInt16u) (time / 60 % 60);
  114.     *second = (CRInt16u) (time % 60);
  115. }
  116.  
  117.  
  118.